package com.ruoyi.web.controller.teacher;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.domain.BasisCaseLoading;
import com.ruoyi.domain.BasisUavTableDt;
import com.ruoyi.service.IBasisCaseLoadingService;
import com.ruoyi.service.IBasisUavTableDtService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 导调人员的专属目标参数Controller
 * 
 * @author wangzebing
 * @date 2025-07-12
 */
@RestController
@RequestMapping("/system/uavTableDt")
public class BasisUavTableDtController extends BaseController
{
    @Autowired
    private IBasisUavTableDtService basisUavTableDtService;

    @Autowired
    private IBasisCaseLoadingService basisCaseLoadingService;

    @Value("${snapshotKS.path}")
    private String snapshotKSPath;

    /**
     * 查询导调人员的专属目标参数列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisUavTableDt basisUavTableDt)
    {
        startPage();
        List<BasisUavTableDt> list = basisUavTableDtService.selectBasisUavTableDtList(basisUavTableDt);
        return getDataTable(list);
    }

    /**
     * 导出导调人员的专属目标参数列表
     */
    @Log(title = "导调人员的专属目标参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisUavTableDt basisUavTableDt)
    {
        List<BasisUavTableDt> list = basisUavTableDtService.selectBasisUavTableDtList(basisUavTableDt);
        ExcelUtil<BasisUavTableDt> util = new ExcelUtil<BasisUavTableDt>(BasisUavTableDt.class);
        util.exportExcel(response, list, "导调人员的专属目标参数数据");
    }

    /**
     * 获取导调人员的专属目标参数详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basisUavTableDtService.selectBasisUavTableDtById(id));
    }

    /**
     * 新增导调人员的专属目标参数
     */
    @Log(title = "导调人员的专属目标参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisUavTableDt basisUavTableDt)
    {
        return toAjax(basisUavTableDtService.insertBasisUavTableDt(basisUavTableDt));
    }

    /**
     * 修改导调人员的专属目标参数
     */
    @Log(title = "导调人员的专属目标参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisUavTableDt basisUavTableDt)
    {

        int i = basisUavTableDtService.updateBasisUavTableDt(basisUavTableDt);
        try {
            String filepath = createJson(basisUavTableDt.getLoadingId());
            return success(filepath);
        }catch (Exception e){
            e.printStackTrace();
            return toAjax(0);
        }
    }

    //导调角色修改目标参数后生成的文件
    public String createJson(String id){
        BasisCaseLoading position = basisCaseLoadingService.findPositionDT(id);

        Gson gson = new GsonBuilder()
                .setPrettyPrinting() // 格式化输出
                .create();

        // 3. 将List转换为JSON字符串
        String json = gson.toJson(position);

        // 4. 写入文件
        try {
            File file = new File(snapshotKSPath);
            if (!file.exists()){
                file.mkdirs();
            }

            String filePath = snapshotKSPath+id+"DT.json";

            Files.write(Paths.get(filePath), json.getBytes());
            System.out.println("Gson生成JSON文件成功: " + filePath);
        } catch (Exception e) {
            System.err.println("文件写入失败: " + e.getMessage());
        }

        return snapshotKSPath+id+"DT.json";
    }

    /**
     * 删除导调人员的专属目标参数
     */
    @Log(title = "导调人员的专属目标参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basisUavTableDtService.deleteBasisUavTableDtByIds(ids));
    }


    @GetMapping(value = "/findByLoadingIdfAndBusinessInformationFormUavTableDT")
    public AjaxResult findByLoadingIdfAndBusinessInformationFormUavTableDT(String loadingId)
    {
        return success(basisUavTableDtService.findByLoadingIdfAndBusinessInformationFormUavTableDT(loadingId));
    }



    @GetMapping("/Dtdownload")
    public ResponseEntity<InputStreamResource> fileDownload(String filePath,String fileName) throws IOException {
        //filePath = "D:\\22.rar";
        FileInputStream fileInputStream = new FileInputStream(filePath);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+fileName)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new InputStreamResource(fileInputStream));
    }
}
